<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="96px">
      <el-form-item label="提现用户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入提现用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现单号" prop="applySn">
        <el-input
          v-model="queryParams.applySn"
          placeholder="请输入提现单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="提现积分" prop="money">-->
<!--        <el-input-->
<!--          v-model="queryParams.money"-->
<!--          placeholder="请输入提现积分"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="货币标准符号" prop="currency">
        <el-input
          v-model="queryParams.currency"
          placeholder="请输入货币标准符号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="积分兑换比例" prop="exchangeRate">-->
<!--        <el-input-->
<!--          v-model="queryParams.exchangeRate"-->
<!--          placeholder="请输入积分兑换比例"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="提现金额" prop="payMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.payMoney"-->
<!--          placeholder="请输入提现金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实际到账" prop="actualMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.actualMoney"-->
<!--          placeholder="请输入实际到账"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="手续费" prop="chargeMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.chargeMoney"-->
<!--          placeholder="请输入手续费"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="手续费率" prop="serviceFee">-->
<!--        <el-input-->
<!--          v-model="queryParams.serviceFee"-->
<!--          placeholder="请输入手续费率"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="打款信息" prop="applyInfo">-->
<!--        <el-input-->
<!--          v-model="queryParams.applyInfo"-->
<!--          placeholder="请输入打款信息"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="平台" prop="platform">-->
<!--        <el-input-->
<!--          v-model="queryParams.platform"-->
<!--          placeholder="请输入平台"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="申请时间" prop="createtime">-->
<!--        <el-input-->
<!--          v-model="queryParams.createtime"-->
<!--          placeholder="请输入申请时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作时间" prop="updatetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.updatetime"-->
<!--          placeholder="请输入操作时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:apply:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="${comment}" align="center" prop="siteId" />-->
      <el-table-column label="提现用户id" align="center" prop="userId" />
      <el-table-column label="提现单号" align="center" prop="applySn" width="300px" />
      <el-table-column label="收款类型" align="center" prop="applyType" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.applyType==='bank'" type="info">银行卡</el-tag>
          <el-tag v-else-if="scope.row.applyType==='wechat'" type="success">微信</el-tag>
          <el-tag v-else-if="scope.row.applyType==='alipay'">支付宝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提现积分" align="center" prop="money" />
<!--      <el-table-column label="货币标准符号" align="center" prop="currency" />-->
      <el-table-column label="积分兑换比例" width="180px" align="center" prop="exchangeRate" >
        <template slot-scope="scope">
          <span v-if="scope.row.currency != null && scope.row.currency != ''"> 1 {{ scope.row.currency }} = {{ scope.row.exchangeRate }} 积分 </span>
        </template>
      </el-table-column>
<!--      <el-table-column label="提现金额" align="center" prop="payMoney" />-->
<!--      <el-table-column label="实际到账" align="center" prop="actualMoney" />-->
      <el-table-column label="手续费" align="center" prop="chargeMoney" >
        <template slot-scope="scope">
          <span>{{ scope.row.chargeMoney }}</span>
          /
          <span>{{ scope.row.serviceFee }} %</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="手续费率" align="center" prop="serviceFee" />-->
<!--      <el-table-column label="打款信息" align="center" prop="applyInfo" />-->
      <el-table-column label="提现状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status=='-1'" type="danger">已拒绝</el-tag>
          <el-tag v-else-if="scope.row.status=='0'" type="info">待审核</el-tag>
          <el-tag v-else-if="scope.row.status=='1'" type="warning">处理中</el-tag>
          <el-tag v-else-if="scope.row.status=='2'" type="success">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="平台" align="center" prop="platform" />
<!--      <el-table-column label="交易原始数据" align="center" prop="paymentJson" />-->
<!--      <el-table-column label="申请时间" align="center" prop="createtime" />-->
<!--      <el-table-column label="操作时间" align="center" prop="updatetime" />-->
<!--      <el-table-column label="操作日志" align="center" prop="log" />-->
      <el-table-column label="操作" width="160px" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <span v-if="scope.row.status != '2'">
              <el-button
                v-if="scope.row.status == '0'"
                size="mini"
                type="text"
                @click="agreeAndPayment(scope.row, 'agree')"
              >同意</el-button>
              <el-button
                v-if="scope.row.status == '1'"
                size="mini"
                type="text"
                @click="handleInfo(scope.row, 'payment')"
              >立即打款</el-button>
              <el-button
                size="mini"
                type="text"
                @click="agreeAndPayment(scope.row, 'refuse')"
              >拒绝</el-button>
            </span>
            <el-button
              :style="{ 'margin-left': scope.row.status != '2' ? '10px' : '0' }"
              size="mini"
              type="text"
              icon="el-icon-time"
              @click="showLog(scope.row.log)"
            ></el-button>
            <!--          <el-button-->
            <!--            size="mini"-->
            <!--            type="text"-->
            <!--            icon="el-icon-delete"-->
            <!--            @click="handleDelete(scope.row)"-->
            <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户提现对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="提现用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入提现用户" />
        </el-form-item>
        <el-form-item label="提现单号" prop="applySn">
          <el-input v-model="form.applySn" placeholder="请输入提现单号" />
        </el-form-item>
        <el-form-item label="提现积分" prop="money">
          <el-input v-model="form.money" placeholder="请输入提现积分" />
        </el-form-item>
        <el-form-item label="货币标准符号" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入货币标准符号" />
        </el-form-item>
        <el-form-item label="积分兑换比例" prop="exchangeRate">
          <el-input v-model="form.exchangeRate" placeholder="请输入积分兑换比例" />
        </el-form-item>
        <el-form-item label="提现金额" prop="payMoney">
          <el-input v-model="form.payMoney" placeholder="请输入提现金额" />
        </el-form-item>
        <el-form-item label="实际到账" prop="actualMoney">
          <el-input v-model="form.actualMoney" placeholder="请输入实际到账" />
        </el-form-item>
        <el-form-item label="手续费" prop="chargeMoney">
          <el-input v-model="form.chargeMoney" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="手续费率" prop="serviceFee">
          <el-input v-model="form.serviceFee" placeholder="请输入手续费率" />
        </el-form-item>
        <el-form-item label="打款信息" prop="applyInfo">
          <el-input v-model="form.applyInfo" placeholder="请输入打款信息" />
        </el-form-item>
        <el-form-item label="平台" prop="platform">
          <el-input v-model="form.platform" placeholder="请输入平台" />
        </el-form-item>
        <el-form-item label="交易原始数据" prop="paymentJson">
          <el-input v-model="form.paymentJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="操作日志" prop="log">
          <el-input v-model="form.log" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="agreeTitle" :visible.sync="agreeOpen" width="500px" append-to-body>
      <span v-if="isAgree">请选择以下操作。</span>
      <el-input v-else v-model="refuseInfo" placeholder="请输入拒绝理由"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="agreeOpen = false">取 消</el-button>
        <el-button v-if="isAgree" type="primary" @click="handleInfo(updApply, 'agree')">同 意</el-button>
        <el-button v-if="isAgree" type="success" @click="handleInfo(updApply, 'agreeToPay')">同意并打款</el-button>
        <el-button v-else type="danger" @click="handleInfo(updApply, 'refuse')">拒绝</el-button>
      </div>
    </el-dialog>

    <el-dialog title="操作日志" :visible.sync="logOpen" width="600px" append-to-body>
      <el-table :data="logList" border>
        <el-table-column label="操作日志" prop="oper_info"></el-table-column>
        <el-table-column label="操作人类型" width="100px" align="center" prop="oper_type"></el-table-column>
        <el-table-column label="操作人id" width="90px" align="center" prop="oper_id"></el-table-column>
        <el-table-column label="操作时间" width="160px" align="center" prop="oper_time">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.oper_time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listApply, getApply, delApply, addApply, updateApply, rechargeUserMoney } from "@/api/user/apply";

export default {
  name: "Apply",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户提现表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        userId: null,
        applySn: null,
        applyType: null,
        money: null,
        currency: null,
        exchangeRate: null,
        payMoney: null,
        actualMoney: null,
        chargeMoney: null,
        serviceFee: null,
        applyInfo: null,
        status: null,
        platform: null,
        paymentJson: null,
        createtime: null,
        updatetime: null,
        log: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "提现用户不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "提现积分不能为空", trigger: "blur" }
        ],
        currency: [
          { required: true, message: "货币标准符号不能为空", trigger: "blur" }
        ],
        exchangeRate: [
          { required: true, message: "积分兑换比例不能为空", trigger: "blur" }
        ],
        payMoney: [
          { required: true, message: "提现金额不能为空", trigger: "blur" }
        ],
        actualMoney: [
          { required: true, message: "实际到账不能为空", trigger: "blur" }
        ],
        chargeMoney: [
          { required: true, message: "手续费不能为空", trigger: "blur" }
        ],
      },
      agreeTitle: '',
      agreeOpen: false,
      isAgree: true,
      updApply: {},
      refuseInfo: '',
      logOpen: false,
      logList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户提现列表 */
    getList() {
      this.loading = true;
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        userId: null,
        applySn: null,
        applyType: null,
        money: null,
        currency: null,
        exchangeRate: null,
        payMoney: null,
        actualMoney: null,
        chargeMoney: null,
        serviceFee: null,
        applyInfo: null,
        status: null,
        platform: null,
        paymentJson: null,
        createtime: null,
        updatetime: null,
        log: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户提现";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户提现";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户提现编号为"' + ids + '"的数据项？').then(function() {
        return delApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/apply/export', {
        ...this.queryParams
      }, `apply_${new Date().getTime()}.xlsx`)
    },
    agreeAndPayment(row, type) {
      this.agreeTitle = '操作人：' + this.$store.state.user.name;
      this.agreeOpen = true;
      this.updApply = row;
      if ('refuse' == type) {
        this.isAgree = false;
      }
    },
    handleInfo(row, type) {
      let jsonObject = JSON.parse(row.log);
      let time = new Date().getTime();
      if ('agreeToPay' == type) {
        let agree = {
          "oper_type": this.$store.state.user.roles[0],
          "oper_id": this.$store.state.user.id,
          "oper_info": "同意提现申请",
          "oper_time": time
        };
        let pay = {
          "oper_type": this.$store.state.user.roles[0],
          "oper_id": this.$store.state.user.id,
          "oper_info": "已打款",
          "oper_time": time
        };
        jsonObject[1] = agree;
        jsonObject[2] = pay;
        row.log = JSON.stringify(jsonObject);
        this.$modal.confirm('是否同意打款？').then(function() {
          return updateApply(row);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("已同意打款");
        }).catch(() => {});
      } else if ('agree' == type) {
        let agree = {
          "oper_type": this.$store.state.user.roles[0],
          "oper_id": this.$store.state.user.id,
          "oper_info": "同意提现申请",
          "oper_time": time
        };
        jsonObject[1] = agree;
        row.log = JSON.stringify(jsonObject);
        this.$modal.confirm('是否立即打款？').then(function() {
          return updateApply(row);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("已同意");
        }).catch(() => {});
      } else if ('payment' == type) {
        let pay = {
          "oper_type": this.$store.state.user.roles[0],
          "oper_id": this.$store.state.user.id,
          "oper_info": "已打款",
          "oper_time": time
        };
        jsonObject[2] = pay;
        row.log = JSON.stringify(jsonObject);
        this.$modal.confirm('是否立即打款？').then(function() {
          return updateApply(row);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("打款成功");
        }).catch(() => {});
      } else if ('refuse' == type) {
        let refuse = {
          "oper_type": this.$store.state.user.roles[0],
          "oper_id": this.$store.state.user.id,
          "oper_info": "同意提现申请",
          "oper_time": time
        };
        jsonObject[1] = refuse;
        row.log = JSON.stringify(jsonObject);
        this.$modal.confirm('是否确认拒绝该提现申请？').then(function() {
          return updateApply(row);
        }).then(() => {
          let params = {
            money: row.money,
            id: row.userId
          }
          rechargeUserMoney(params).then(response => {
            this.getList();
            this.$modal.msgSuccess("拒绝成功");
          })
        }).catch(() => {});
      }
    },
    showLog(jsonStr) {
      let jsonObject = JSON.parse(jsonStr);
      this.logList = jsonObject;
      this.logOpen = true;
    }
  }
};
</script>
